/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataBase;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daniel Cervantes Avila
 */
public class Transaccion {
    private Connection connection;

    public Transaccion(Connection connection){
        this.connection=connection;
    }

    public void iniciar() throws SQLException{
        connection.setAutoCommit(false);
    }
    public void finalizar() throws SQLException{
        connection.commit();
        connection.setAutoCommit(true);
        connection.close();
    }

    public void rollBack(){
        try {
            connection.rollback();
            connection.setAutoCommit(true);
            connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(Transaccion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PreparedStatement preparedStatement(String sql) throws SQLException{
        return connection.prepareStatement(sql);
    }
    public PreparedStatement preparedStatementWithKey(String sql) throws SQLException{
        return connection.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
    }
    public void executeUpdate(PreparedStatement ps,List<Object> params) throws SQLException{
        int x=1;
        if(params!=null){
        for(Object param:params){
            if(param==null)ps.setNull(x,java.sql.Types.JAVA_OBJECT);
            else if(param.toString().equals(""))ps.setNull(x,java.sql.Types.JAVA_OBJECT);
            else if(param.getClass().getSimpleName().equals("String")) ps.setString(x, param.toString());
            else if(param.getClass().getSimpleName().equals("Integer")) ps.setInt(x,Integer.parseInt(param.toString()));
            else if(param.getClass().getSimpleName().equals("Double")) ps.setDouble(x,Double.parseDouble(param.toString()));
            else if(param.getClass().getSimpleName().equals("Long")) ps.setLong(x,Long.parseLong(param.toString()));
           else if(param.getClass().getSimpleName().equals("BigDecimal")) ps.setBigDecimal(x,(BigDecimal)param);
            else if(param.getClass().getSimpleName().equals("Timestamp")) ps.setTimestamp(x, (Timestamp)param);
            x++;
        }
        }
        System.out.println(ps.toString());
        ps.executeUpdate();
    }
    public int executeUpdateWithKey(PreparedStatement ps,List<Object> params) throws SQLException{
        int x=1;
        if(params!=null){
        for(Object param:params){
            if(param==null)ps.setNull(x,java.sql.Types.JAVA_OBJECT);
            else if(param.toString().equals("")) ps.setNull(x,java.sql.Types.JAVA_OBJECT);
            else if(param.getClass().getSimpleName().equals("String"))ps.setString(x, param.toString());
            else if(param.getClass().getSimpleName().equals("Integer"))ps.setInt(x,Integer.parseInt(param.toString()));
            else if(param.getClass().getSimpleName().equals("Double"))ps.setDouble(x,Double.parseDouble(param.toString()));
            else if(param.getClass().getSimpleName().equals("Longger"))ps.setLong(x,Long.parseLong(param.toString()));
            else if(param.getClass().getSimpleName().equals("BigDecimal")) ps.setBigDecimal(x,(BigDecimal)param);
            else if(param.getClass().getSimpleName().equals("Timestamp")) ps.setTimestamp(x, (Timestamp)param);
            x++;
        }
        }
        System.out.println(ps.toString());
        ps.executeUpdate();
        ResultSet rs = ps.getGeneratedKeys();
        while (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    }

}






